package cyce.file;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.Serializable;

/**
 * Class to represent a File object that can be sent
 * and recreated on another system
 * Will actually do the creating for you
 **/
public class FilePacket implements Serializable 
{
	private static final long serialVersionUID = -7488903441137894253L;

	private String m_filePath;
	private byte[] m_data;

	/**
	 * Make a file packet that represents a given filename
	 * @param name The filename this represents
	 **/
	public FilePacket( String filePath )
	{
		m_filePath = filePath;
	}

	/**
	 * Get the name associated with this file
	 * @return The name
	 **/
  	public String getFullName()
  	{
  		return m_filePath;
  	}

	/**
	 * Have the FilePacket read itself in from the
	 * file it represents in name
	 **/
  	public void readIn() throws FileNotFoundException
  	{
	  	try
	  	{
	    	java.io.File file = new java.io.File( getFullName() );
	     	m_data = new byte[ (int)(file.length()) ];
	     	(new FileInputStream( file )).read( m_data );
	    }
	  	catch( Exception e )
	  	{
	    	e.printStackTrace();
	   	}
	}

	/**
	 * Have the file packet recreate itself, used
	 * after sending it to another location
	 * file will have same name and contents
	 * @param out The outputStream to write itself to
	 **/
	public void writeTo( OutputStream out )
	{
		try
		{
			out.write( m_data );
		}
		catch( Exception e )
		{
			e.printStackTrace();
		}
	}
}